#-*- coding:UTF-8 -*-
'''
Created on 2013-6-5

@author: hanjiewu
'''
#except (Exception1, Exception2)[, reason]:
import os
'''
Exception 是父异常类
异常的继承结构
BaseException
    KeyboardInterrupt
    SystemExit
    Exception
        all other current build Exception
'''

try:
    pass
except Exception:
    pass

def safe_float(obj):
    'safe version of float()'
    try:
        retval = float(obj)
    except (ValueError, TypeError), diag:
        retval = str(diag)
    return retval
def main():
    'handles all the data processing'
    log = open('cardlog.txt', 'w')
    try:
        ccfile = open(os.path.dirname(os.path.dirname(os.getcwd())) + os.sep + 'data' + os.sep + 'carddata.txt', 'r')
    except IOError, e:
        log.write('no txns this month\n')
        log.close()
        return
    txns = ccfile.readlines()
    ccfile.close()
    total = 0.00
    log.write("account log:\n")
    
    for eachTxn in txns:
        result = safe_float(eachTxn)
        if isinstance(result, float):
            total += result
            log.write('data... processed\n')
        else:
            log.write('ignored:%s' % result)
    print '$%.2f (new balance)' % (total)
    
    log.close()

if __name__ == '__main__':
    main()
    
    